package com.noble.meether.chat.ui.imui.view;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.widget.ImageButton;
import android.widget.RelativeLayout;

import com.noble.meether.R;
import com.noble.imui.chatinput.ChatInputView;
import com.noble.imui.chatinput.listener.OnCameraCallbackListener;
import com.noble.imui.chatinput.listener.OnClickEditTextListener;
import com.noble.imui.chatinput.listener.OnMenuClickListener;
import com.noble.imui.chatinput.listener.RecordVoiceListener;
import com.noble.imui.chatinput.record.RecordVoiceButton;
import com.noble.imui.messagelist.messages.MessageList;
import com.noble.imui.messagelist.messages.MsgListAdapter;
import com.noble.imui.messagelist.messages.ptr.PtrDefaultHeader;
import com.noble.imui.messagelist.messages.ptr.PullToRefreshLayout;
import com.noble.imui.messagelist.utils.DisplayUtil;

public class ChatView extends RelativeLayout {
    private MessageList mMsgList;
    private ChatInputView mChatInput;           // 输入控件
    private RecordVoiceButton mRecordVoiceBtn;  // 语音录制
    private PullToRefreshLayout mPtrLayout;
    private ImageButton mSelectAlbumIb;

    public ChatView(Context context) {
        super(context);
    }

    public ChatView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ChatView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void initModule() {
        mMsgList = findViewById(R.id.msg_list);
        mChatInput = findViewById(R.id.chat_input_view);
        mPtrLayout = findViewById(R.id.pull_to_refresh_layout);

        // TODO 此处最好设置为软键盘高度，否则会导致第一次打开菜单时高度不正常（此时打开软键盘会导致界面伸缩）
        // 建议在跳转到聊天界面之前使用 onSizeChanged 方法监听软键盘的高度，然后在初始化的时候设置即可。
        mChatInput.setMenuContainerHeight(500);
        mRecordVoiceBtn = mChatInput.getRecordVoiceButton();
        mSelectAlbumIb = mChatInput.getSelectAlbumBtn();

        // 消息列表
        PtrDefaultHeader header = new PtrDefaultHeader(getContext());
        int[] colors = getResources().getIntArray(R.array.google_colors);
        header.setColorSchemeColors(colors);
        header.setLayoutParams(new LayoutParams(-1, -2));
        header.setPadding(0, DisplayUtil.dp2px(getContext(), 15), 0,
                DisplayUtil.dp2px(getContext(), 10));
        header.setPtrFrameLayout(mPtrLayout);
//        mMsgList.setDateBgColor(Color.parseColor("#FF4081"));
//        mMsgList.setDatePadding(5, 10, 10, 5);
//        mMsgList.setEventTextPadding(5);
//        mMsgList.setEventBgColor(Color.parseColor("#34A350"));
//        mMsgList.setDateBgCornerRadius(15);
        mMsgList.setHasFixedSize(true);
//        mMsgList.setShowReceiverDisplayName(true);  // 接收方显示昵称
//        mMsgList.setShowSenderDisplayName(false);   // 发送方显示昵称

        // 下拉刷新组件
        mPtrLayout.setLoadingMinTime(1000);
        mPtrLayout.setDurationToCloseHeader(1500);
        mPtrLayout.setHeaderView(header);
        mPtrLayout.addPtrUIHandler(header);
        // 下拉刷新时，内容固定，只有 Header 变化
        mPtrLayout.setPinContent(true);

    }

    // ===================================== 获取组件 ================================
    public ChatInputView getChatInputView() {
        return mChatInput;
    }

    public MessageList getMessageListView() {
        return mMsgList;
    }

    public ImageButton getSelectAlbumBtn() {
        return this.mSelectAlbumIb;
    }

    // ===================================== ChatInputView 监听事件 ================================

    /**
     * 输入框下面的菜单栏事件监听
     */
    public void setMenuClickListener(OnMenuClickListener listener) {
        mChatInput.setMenuClickListener(listener);
    }

    /**
     * @param listener 监听输入框点击事件，点击输入框后触发此事件
     */
    public void setOnTouchEditTextListener(OnClickEditTextListener listener) {
        mChatInput.setOnClickEditTextListener(listener);
    }

    /**
     * @param listener 录音事件监听
     */
    public void setRecordVoiceListener(RecordVoiceListener listener) {
        mChatInput.setRecordVoiceListener(listener);
    }

    /**
     * @param listener 相机相关的接口
     */
    public void setOnCameraCallbackListener(OnCameraCallbackListener listener) {
        mChatInput.setOnCameraCallbackListener(listener);
    }


    // ============================= 以下暂时保留 ===============
    public PullToRefreshLayout getPtrLayout() {
        return mPtrLayout;
    }

    public void setAdapter(MsgListAdapter adapter) {
        mMsgList.setAdapter(adapter);
    }

    public void setLayoutManager(RecyclerView.LayoutManager layoutManager) {
        mMsgList.setLayoutManager(layoutManager);
    }

    public void setRecordVoiceFile(String path, String fileName) {
        mRecordVoiceBtn.setVoiceFilePath(path, fileName);
    }

    public void setCameraCaptureFile(String path, String fileName) {
        mChatInput.setCameraCaptureFile(path, fileName);
    }

    public void setOnTouchListener(OnTouchListener listener) {
        mMsgList.setOnTouchListener(listener);
    }

    @Override
    public boolean performClick() {
        super.performClick();
        return true;
    }
}
